package com.hunttown.mes.common.utils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

/**
 * created by wangjunfu on 2018-10-29
 */
public class CookieUtils {

    /**
     * 保存Cookies
     *
     * @param response    servlet请求
     * @param cookiename  cookie 名称
     * @param cookievalue cookie 值
     * @param time        过期时间 单位：秒
     */
    public static void setCookie(HttpServletResponse response, String cookiename, String cookievalue, int time) {
        try {
            // 如果cookie的值中含有中文时，需要对cookie进行编码，不然会产生乱码
            cookievalue = URLEncoder.encode(cookievalue, "utf-8");
        } catch (Exception e) {
            e.printStackTrace();
        }

        Cookie cookie = new Cookie(cookiename, cookievalue);
        cookie.setPath("/");

        //1.负数，只要关闭浏览器，此cookie就会消失。
        //2.0，表示立刻从客户端电脑或浏览器内存中删除此cookie。
        //3.正数，表示该Cookie会在maxAge秒之后自动失效。
        cookie.setMaxAge(time);

        // 保存cookie, 如果已经存在相同名字的cookie，则最新的覆盖旧的cookie
        response.addCookie(cookie);
    }

    /**
     * 根据名字获取cookie
     *
     * @param request    servlet请求
     * @param cookiename cookie名字
     * @return
     */
    public static String getCookieByName(HttpServletRequest request, String cookiename) {
        String cookievalue = null;

        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals(cookiename)) {
                    cookievalue = cookie.getValue();
                    break;
                }
            }
        }

        if (cookievalue != null) {
            try {
                // 保存的时候进行encode
                cookievalue = URLDecoder.decode(cookievalue, "utf-8");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return cookievalue;
    }

    /**
     * 根据名字获取cookie
     *
     * @param request    servlet请求
     * @param cookiename cookie名字
     * @return
     */
    public static Cookie getCookie(HttpServletRequest request, String cookiename) {
        Map<String, Cookie> cookieMap = ReadCookieMap(request);
        if (cookieMap.containsKey(cookiename)) {
            Cookie cookie = (Cookie) cookieMap.get(cookiename);
            return cookie;
        } else {
            return null;
        }
    }

    /**
     * 将cookie封装到map里面
     *
     * @param request servlet请求
     * @return
     */
    private static Map<String, Cookie> ReadCookieMap(HttpServletRequest request) {
        Map<String, Cookie> cookieMap = new HashMap<String, Cookie>();
        Cookie[] cookies = request.getCookies();
        if (null != cookies) {
            for (Cookie cookie : cookies) {
                cookieMap.put(cookie.getName(), cookie);
            }
        }
        return cookieMap;
    }
}